home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / ispell40.lha / ispell-4.0 / freq.c < prev    next >
C/C++ Source or Header  |  1993-05-31  |  2KB  |  106 lines

  1. /* Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  2.  
  3.    This file is part of GNU ISPELL.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* compute the frequencies of character pairs in a file
  20.    then write a list of characters used, plus a sorted list
  21.    of frequencies
  22.  
  23.    this program needs to run on 80286's, so it doesn't use
  24.    any individual data structure larger than 64k. */
  25.  
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include "charset.h"
  29.  
  30. /* this array contains letters to use when generating near misses */
  31. char near_miss_letters[256];
  32. int nnear_miss_letters;
  33.  
  34. /* this array has 1 for any character that is in near_miss_letters */
  35. char near_map[256];
  36.  
  37. long *table[256];
  38. char used[256];
  39.  
  40. int
  41. main ()
  42. {
  43.   int c1, c2;
  44.   long count;
  45.   int i, j;
  46.   FILE *out;
  47.  
  48.   if ((out = popen ("sort -nr", "w")) == NULL)
  49.     {
  50.       fprintf (stderr, "can't make pipe to sort\n");
  51.       exit (1);
  52.     }
  53.  
  54.   for (i = 0; i < 256; i++)
  55.     table[i] = (long *) xcalloc (256, sizeof (long));
  56.  
  57.   c1 = 0;
  58.   while (c1 == 0)
  59.     {
  60.       c1 = getchar ();
  61.       if (c1 == EOF)
  62.     {
  63.       fprintf (stderr, "no input\n");
  64.       exit (1);
  65.     }
  66.       c1 = charset[c1].lowercase;
  67.     }
  68.   used[c1] = 1;
  69.  
  70.   while ((c2 = getchar ()) != EOF)
  71.     {
  72.       c2 = charset[c2].lowercase;
  73.       if (c2 == 0)
  74.     continue;
  75.       used[c2] = 1;
  76.       table[c1][c2]++;
  77.       c1 = c2;
  78.     }
  79.  
  80.   /* a big "count" so this line will be at the top of the
  81.      * sorted output
  82.      */
  83.   fprintf (out, "30000 ");
  84.   for (c1 = 0; c1 < 256; c1++)
  85.     if (used[c1])
  86.       putc (c1, out);
  87.   putc ('\n', out);
  88.  
  89.   for (i = 0; i < 256; i++)
  90.     {
  91.       if (used[i] == 0)
  92.     continue;
  93.       for (j = 0; j < 256; j++)
  94.     {
  95.       if (used[j] == 0)
  96.         continue;
  97.       count = table[i][j];
  98.       if (count)
  99.         fprintf (out, "%ld %c%c\n", count, i, j);
  100.     }
  101.     }
  102.  
  103.   pclose (out);
  104.   return 0;
  105. }
  106.